# -*- coding: utf-8 -*-
import base_object
import t_cmd
import cg_network
import cPickle
import t_room_logic
import t_mapmgr

inst = None

#模块初始化函数
def init():
	global inst
	if not inst:
		inst = CCommand()

#模块自清理函数，释放引用
def destroy():
	global inst
	if inst:
		inst.destroy()
		inst = None

#网络消息打包和发送的类，所有客户端向服务端发送的消息都在这里打包和发送
class CCommand(base_object.CObject):
	def __init__(self):
		super(CCommand, self).__init__()

	#自清理函数，因为这个对象没有成员变量，所有也不需要释放什么东西
	def destroy(self):
		pass

	#选择关卡消息的打包发送函数：iStage为关卡的id
	def send_select_stage(self, iStage):
		cg_network.sender.game_c_select_stage(stage=iStage)

	#准备/取消准备消息的打包发送函数：flag=1代表准备完毕，flag=0代表取消准备
	def send_game_ready(self, flag):
		cg_network.sender.game_c_game_ready(is_ready=flag)

	#离开房间消息的打包发送函数：无参数
	def send_leave_game(self):
		cg_network.sender.game_c_leave_game(no_use=0)
	#单机用的结束游戏
	def send_game_end(self,flag):
		cg_network.sender.game_c_game_end(iswin=flag)

	#踢人
	def send_game_kick(self,obj):
		cg_network.sender.game_c_game_kick(hid=obj.hid)
	#换座位
	def send_game_side(self,iuid,old,new):
		cg_network.sender.game_c_game_side(uid=iuid,old_side=old,new_side=new)

	#换房主
	def send_game_master(self,imaster):
		cg_network.sender.game_c_game_master(master=imaster)

	#巫师指令的打包发送函数：cmds是巫师指令id；param_list是具体的指令信息（根据id不同，长度可变的int列表）
	def send_wizard(self, cmds, param_list):
		int_list = []
		for key in param_list:
			obj = cg_network.get_game_room_msgmgr().game_single_int(value=key)
			int_list.append(obj)
		cg_network.sender.game_c_wizard(head=cmds, param=int_list)

	#游戏初始化ok
	def send_game_init(self, flag):
		print "send_game_init_deno!"
		cg_network.sender.game_c_game_init(binit=flag)

	#游戏内子事件的打包发送函数：event是子事件的id；param_list是子事件的具体内容
	def send_game_event(self, event, param_list):
		_str = cPickle.dumps(param_list)
		cg_network.sender.game_c_game_event(frame=0, event_id=event, param=_str)
#-------------------------发送消息给服务器
	# 旋转
	def send_turend(self, rotate):
		param = (rotate)
		self.send_game_event(t_cmd.C_TUREND, param)

	# 玩家移动状态
	def send_move(self, ismove, dir, pos, topos = False):
		param = (ismove, dir, pos, topos)
		self.send_game_event(t_cmd.C_GAME_MOVE, param)

	# 击打宝石(位置,方向)
	def send_hitgem(self, grid, dir):
		param = (grid, dir)
		self.send_game_event(t_cmd.C_GAME_HITGEM, param)

	# 移动指定位置
	def send_run(self, posx, posy):
		param = (posx, posy)
		self.send_game_event(t_cmd.C_RUN, param)

	# 攻击
	def send_atk(self, pos, dir, weapon):
		param = (pos, dir, weapon)
		self.send_game_event(t_cmd.C_ATK, param)

	# 发送拾取物品消息 参数(物品名称)
	def send_pickitem(self, names):
		param = (names)
		self.send_game_event(t_cmd.C_PICKITEM, param)

	# 互动点击(点击了地点相关的互动物) (地点,类型)
	def send_hudong_click(self, city, type):
		param = (city, type)
		self.send_game_event(t_cmd.C_HDCLICK, param)
		pass

	# 点击接收了任务
	def send_accept_task(self, city, id):
		param = (city, id)
		self.send_game_event(t_cmd.C_ACCEPT_TASK, param)

	# 玩家主动进入世界地图战斗场景
	def send_inworld_grid(self, grid):
		param = (grid,)
		self.send_game_event(t_cmd.C_IN_WORLD_ATK, param)
#----------------------------------------------------------------------